"""
@Author  : 康帅
@Time    : 2021/5/7 17:22
@Function: 如何流式读取数G超大文件
"""
from functools import partial


def read_from_file(filename, block_size=1024 * 8):
    """ 每次只读取 8kb 返回 """
    with open(filename, "r") as fp:
        while True:
            chunk = fp.read(block_size)
            if not chunk:
                break

            yield chunk


def read_from_file_2(filename, block_size=1024 * 8):
    """  使用偏函数 """
    with open(filename, "r") as fp:
        for chunk in iter(partial(fp.read, block_size), ""):
            yield chunk


def read_from_file_3(filename, block_size=1024 * 8):
    """ python3.8+ 使用此方法 """
    with open(filename, "r") as fp:
        while chunk := fp.read(block_size):
            yield chunk


if __name__ == '__main__':
    pass
